<script lang="ts" setup name="SealCity">
import { ref, watch } from "vue";
import { onClickOutside } from "@vueuse/core";
import axios from "axios";

defineProps<{
  address: string;
}>();
const emits = defineEmits<{
  changeCity: [city: ChangeResult];
}>();

const active = ref(false);

const target = ref(null);
onClickOutside(target, () => {
  active.value = false;
});

// 注意：city组件要保持独立性，数据最好都放在组件内部
// 发送ajax请求获取省市区数据
// 1.数据存在本地
// 2.数据存在自己的数据库中
// 3.数据存在第三方的接口中(阿里云)
// 城市列表类型
export type AreaList = {
  code: string;
  level: number;
  name: string;
  areaList: AreaList[];
};

const cityList = ref([] as AreaList[]);
const cacheList = ref([] as AreaList[]);
const getCityList = async () => {
  const res = await axios.get(
    "https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json"
  );
  cityList.value = res.data;
  cacheList.value = res.data;
  // console.log(res.data);
};
getCityList();

// 省市区的点击
export type ChangeResult = {
  provinceName: string;
  provinceCode: string;
  cityName: string;
  cityCode: string;
  countyName: string;
  countyCode: string;
};

const changeResult = {
  provinceName: "",
  provinceCode: "",
  cityName: "",
  cityCode: "",
  countyName: "",
  countyCode: "",
};
const selectCity = (city: AreaList) => {
  if (city.level == 0) {
    changeResult.provinceName = city.name;
    changeResult.provinceCode = city.code;
    cityList.value = city.areaList;
  }
  if (city.level == 1) {
    changeResult.cityName = city.name;
    changeResult.cityCode = city.code;
    cityList.value = city.areaList;
  }
  if (city.level == 2) {
    changeResult.countyName = city.name;
    changeResult.countyCode = city.code;
    active.value = false;

    emits("changeCity", changeResult);
  }
};
// active的数据变化，cityList数据还原
watch(active, () => {
  if (!active.value) {
    cityList.value = cacheList.value;
  }
});
</script>

<template>
  <div class="seal-city" ref="target">
    <div class="select" @click="active = !active" :class="{ active: active }">
      <span class="value" v-if="address">{{ address }}</span>
      <span class="placeholder" v-else>请选择配送地址</span>

      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="active">
      <span
        class="ellipsis"
        v-for="item in cityList"
        :key="item.code"
        @click="selectCity(item)"
        >{{ item.name }}</span
      >
    </div>
  </div>
</template>

<style scoped lang="less">
.seal-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
